// pages/enjoy/enjoy.js
var app = getApp()
var restaurantId;
var privilegeId;
var flag=0;
var hasChecked="";
var isFirst='';
var lastVericationTime="";
var cupNum=1;
Page({
  data:{
    array:[1,2,3,4,5,6,7,8,9,10],
    index:0,
    userInfo: {},
    wishUser:{},
    restaurantName:"",
    name:"",
    imgUrl:"",
    toastHidden:true,
    pickerHidden:"hidden",
    dateHidden:true,
    date:"",
    notap:"",
    btnText1:"",
    btnText2:"",
    qurenText:""
  },
  onLoad:function(options){
    flag=0
    console.log(options);
    var that = this
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
    restaurantId = options.restaurantId;
    wx.request({
        url:app.globalData.path+"/weixin/coffee/xcx/coffeePass/hexiao",
        data:{
            restaurantId : restaurantId,
            userId: that.data.userInfo.userId
        },
        success:function(res){
          console.info(res);
            privilegeId = res.data.id;
            hasChecked = res.data.hasChecked;
            isFirst = res.data.first;
            lastVericationTime = res.data.lastVericationTimeStr;
            if(hasChecked == true){
                flag=1;
                that.setData({
                  notap:"notap",
                  btnText:"已享用"
                })
            }
            if(isFirst == false){
              that.setData({
               pickerHidden:"",
               btnText1: res.data.firstMsg,
               btnText2:res.data.otherMsg
              })
            }else{
              that.setData({
                btnText1: res.data.firstMsg,
                btnText2: res.data.otherMsg
              })
            }
            if (lastVericationTime){
              that.setData({
                dateHidden:false,
                date: "您在 " + lastVericationTime + " 消费了 " + res.data.verificationNum + " 杯咖啡"
              })
            }
            that.setData({
                imgUrl:res.data.imgUrl,
                restaurantName:res.data.restaurantName,
                name:res.data.name
            })
        }
    });
  },
  cancel:function(){
    this.setData({
      toastHidden:true
    })
  },
  sureUse:function(){
    if(flag==0){
      this.setData({
        toastHidden:false,
        qurenText:"确认享用此优惠吗？\n请务必由餐厅服务员见证此操作"
      })
    }
  },
  verification:function(){
      var that=this
			this.setData({
            toastHidden:true
       })
			 wx.request({
	            type: "GET",
	            url: app.globalData.path+"/weixin/coffee/xcx/coffeePass/verification",
	            data: {
	            	restaurantId:restaurantId,
                userId: that.data.userInfo.userId,
                cupNum: cupNum
	            	},
	            async:false,
	            dataType: "json",
	            success: function(res){
	            		   wx.showToast({  
                      title: '恭喜您！请享用！',  
                      icon: 'success',  
                      duration:1500  
                      })
                     if (isFirst == true){
                        that.setData({
                          pickerHidden:""
                        })
                        isFirst = false
                      }else{
                       that.setData({
                         notap: "notap",
                         btnText1: "已享用"
                       })
                       flag = 1;
                      }                      
	            	   		that.setData({
                         date: "您在 " + res.data.lastVericationTime + " 消费了 " + res.data.verificationNum + " 杯咖啡"
                       })
	                }
	      })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    cupNum = parseInt(e.detail.value)+1;
    this.setData({
      index: e.detail.value,
      toastHidden: false,
      qurenText: "确认享用" + cupNum + "杯8折优惠吗？\n请务必由餐厅服务员见证此操作"
    })
  }
})